/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon;

import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;
import za.org.foobar.famon.dto.*;

/**
 *
 */
public class Serializer {
    public static String serializeFailure(CFailure failure) {
        JSONSerializer serializer = new JSONSerializer();
        return serializer.exclude("*.class").deepSerialize(failure);
    }

    public static String serializeFailureBatch(CFailureBatch batch) {
        JSONSerializer serializer = new JSONSerializer();
        return serializer.exclude("*.class").deepSerialize(batch);
    }

    public static JSONDeserializer<CFailureBatch> getJSONDeserializer() {
        return useFailures(new JSONDeserializer<CFailureBatch>()
                .use(null, CFailureBatch.class), "");
    }

    public static <T> JSONDeserializer<T> useFailures(JSONDeserializer<T> d, String prefix) {
        return d.use(prefix + "failures", CFailure.class)
                .use(prefix + "failures.threads", CThreadInformation.class)
                .use(prefix + "failures.threads.loggingEvents", CLoggingEvent.class)
                .use(prefix + "failures.threads.stackTrace", CStackTraceElement.class);
    }

    public static CFailureBatch deserializeFailureBatch(String json) {
        return useFailures(new JSONDeserializer<CFailureBatch>()
                .use(null, CFailureBatch.class), "")
                .deserialize(json);
    }

    public static CFailureList deserializeFailureList(String json) {
        return useFailures(new JSONDeserializer<CFailureList>()
                .use(null, CFailureList.class), "")
                .deserialize(json);
    }

}
